home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / ext / events / searchprovider.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  7KB  |  190 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''
  5. For this plugin to work Gnome Shell needs this file:
  6.  
  7. /usr/share/gnome-shell/search-providers/quodlibet-search-provider.ini
  8.  
  9. with the following content:
  10.  
  11. [Shell Search Provider]
  12. DesktopId=quodlibet.desktop
  13. BusName=net.sacredchao.QuodLibet.SearchProvider
  14. ObjectPath=/net/sacredchao/QuodLibet/SearchProvider
  15. Version=2
  16. '''
  17. import os
  18. import sys
  19. if os.name == 'nt' or sys.platform == 'darwin':
  20.     from quodlibet.plugins import PluginNotSupportedError
  21.     raise PluginNotSupportedError
  22. import dbus
  23. import dbus.service as dbus
  24. from quodlibet import app
  25. from quodlibet.util.dbusutils import dbus_unicode_validate
  26. from quodlibet.plugins.events import EventPlugin
  27. from quodlibet.parse import Query
  28. from quodlibet.plugins import PluginImportException
  29. from quodlibet.util.path import xdg_get_system_data_dirs
  30.  
  31. def get_gs_provider_files():
  32.     '''Return all installed search provider files for Gnome Shell'''
  33.     ini_files = []
  34.     for d in xdg_get_system_data_dirs():
  35.         path = os.path.join(d, 'gnome-shell', 'search-providers')
  36.         
  37.         try:
  38.             for entry in os.listdir(path):
  39.                 if entry.endswith('.ini'):
  40.                     ini_files.append(os.path.join(path, entry))
  41.                     continue
  42.         continue
  43.         except EnvironmentError:
  44.             continue
  45.         
  46.  
  47.     
  48.     return ini_files
  49.  
  50.  
  51. def check_ini_installed():
  52.     '''Raise if no Gnome Shell ini file for Quod Libet is found'''
  53.     quodlibet_installed = False
  54.     for path in get_gs_provider_files():
  55.         
  56.         try:
  57.             with open(path, 'rb') as handle:
  58.                 if SearchProvider.BUS_NAME in handle.read():
  59.                     quodlibet_installed = True
  60.                     break
  61.         continue
  62.         except EnvironmentError:
  63.             continue
  64.         
  65.  
  66.     
  67.     if not quodlibet_installed:
  68.         raise PluginImportException(_('No Gnome Shell search provider for Quod Libet installed.'))
  69.  
  70.  
  71. class GnomeSearchProvider(EventPlugin):
  72.     PLUGIN_ID = 'searchprovider'
  73.     PLUGIN_NAME = _('Gnome Search Provider')
  74.     PLUGIN_DESC = _('Allow the Gnome Shell to search the library')
  75.     PLUGIN_ICON = 'gtk-connect'
  76.     PLUGIN_VERSION = '0.1'
  77.     
  78.     def enabled(self):
  79.         self.obj = SearchProvider()
  80.  
  81.     
  82.     def disabled(self):
  83.         self.obj.remove_from_connection()
  84.         del self.obj
  85.         import gc as gc
  86.         gc.collect()
  87.  
  88.  
  89. ENTRY_ICON = '. GThemedIcon audio-mpeg gnome-mime-audio-mpeg audio-x-generic'
  90.  
  91. def get_song_id(song):
  92.     return str(id(song))
  93.  
  94.  
  95. def get_songs_for_ids(library, ids):
  96.     songs = []
  97.     ids = set(ids)
  98.     for song in library:
  99.         song_id = get_song_id(song)
  100.         if song_id in ids:
  101.             songs.append(song)
  102.             ids.discard(song_id)
  103.             if not ids:
  104.                 break
  105.             
  106.     return songs
  107.  
  108.  
  109. class SearchProvider(dbus.service.Object):
  110.     PATH = '/net/sacredchao/QuodLibet/SearchProvider'
  111.     BUS_NAME = 'net.sacredchao.QuodLibet.SearchProvider'
  112.     IFACE = 'org.gnome.Shell.SearchProvider2'
  113.     
  114.     def __init__(self):
  115.         bus = dbus.SessionBus()
  116.         name = dbus.service.BusName(self.BUS_NAME, bus)
  117.         super(SearchProvider, self).__init__(name, self.PATH)
  118.  
  119.     
  120.     def GetInitialResultSet(self, terms):
  121.         if terms:
  122.             query = Query('')
  123.             for term in terms:
  124.                 query &= Query(term)
  125.             
  126.             songs = filter(query.search, app.library)
  127.         else:
  128.             songs = app.library.values()
  129.         ids = [ get_song_id(s) for s in songs ]
  130.         return ids
  131.  
  132.     GetInitialResultSet = dbus.service.method(IFACE, in_signature = 'as', out_signature = 'as')(GetInitialResultSet)
  133.     
  134.     def GetSubsearchResultSet(self, previous_results, terms):
  135.         query = Query('')
  136.         for term in terms:
  137.             query &= Query(term)
  138.         
  139.         songs = get_songs_for_ids(app.library, previous_results)
  140.         ids = [ get_song_id(s) for s in songs if query.search(s) ]
  141.         return ids
  142.  
  143.     GetSubsearchResultSet = dbus.service.method(IFACE, in_signature = 'asas', out_signature = 'as')(GetSubsearchResultSet)
  144.     
  145.     def GetResultMetas(self, identifiers):
  146.         metas = []
  147.         for song in get_songs_for_ids(app.library, identifiers):
  148.             name = song('title')
  149.             description = song('~artist~title')
  150.             song_id = get_song_id(song)
  151.             meta = dbus.Dictionary({
  152.                 'name': dbus_unicode_validate(name),
  153.                 'id': song_id,
  154.                 'description': dbus_unicode_validate(description),
  155.                 'gicon': ENTRY_ICON }, signature = 'ss')
  156.             metas.append(meta)
  157.         
  158.         return metas
  159.  
  160.     GetResultMetas = dbus.service.method(IFACE, in_signature = 'as', out_signature = 'aa{sv}')(GetResultMetas)
  161.     
  162.     def ActivateResult(self, identifier, terms, timestamp):
  163.         
  164.         try:
  165.             app.window.browser.filter_text(' '.join(terms))
  166.         except NotImplementedError:
  167.             pass
  168.  
  169.         songs = get_songs_for_ids(app.library, [
  170.             identifier])
  171.         if not songs:
  172.             return None
  173.         if None.player.go_to(songs[0], True):
  174.             app.player.paused = False
  175.  
  176.     ActivateResult = dbus.service.method(IFACE, in_signature = 'sasu')(ActivateResult)
  177.     
  178.     def LaunchSearch(self, terms, timestamp):
  179.         
  180.         try:
  181.             app.window.browser.filter_text(' '.join(terms))
  182.         except NotImplementedError:
  183.             pass
  184.  
  185.         app.present()
  186.  
  187.     LaunchSearch = dbus.service.method(IFACE, in_signature = 'asu')(LaunchSearch)
  188.  
  189. check_ini_installed()
  190.